home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 8189 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.8 KB

  1. Path: beach.and.nl!usenet
  2. From: jos@and.nl (Jos A. Horsmeier)
  3. Newsgroups: comp.lang.pascal.misc,comp.lang.c++,comp.lang.c,comp.lang.pascal.borland
  4. Subject: Re: Tough FACTORIAL math problem...
  5. Date: 15 Feb 1996 17:00:38 GMT
  6. Organization: AND Operations Research B.V.
  7. Message-ID: <4fvorm$dfm@beach.and.nl>
  8. References: <4fr8be$ass@news.iconn.net> <wzenrwg142.fsf@stego.cs.ruu.nl>
  9. NNTP-Posting-Host: klepzeiker.and.nl
  10. Mime-Version: 1.0
  11. Content-Type: Text/Plain; charset=ISO-8859-1
  12. X-Newsreader: WinVN 0.99.5
  13.  
  14. In article <wzenrwg142.fsf@stego.cs.ruu.nl>, piet@stego.cs.ruu.nl wrote:
  15. |>>>>> thecrow@iconn.net (The Crow) (TC) writes:
  16. |
  17. |TC> Here is what I am trying to do, I want to find the last NON-ZERO digit
  18. |TC> of a given factorial. For instance,
  19. |
  20. |TC> 5! = 120
  21. |
  22. |TC> so the last non-zero digit is 2. I want to be able to do this up to
  23. |TC> 1000. Problem is, no matter how huge of a data-type you use, there
  24. |TC> isn't any way for the computer to handle 1000!, it is however possible
  25.  
  26. |A little thinking before coding solves this problem very easily:
  27. |
  28. |Let N be then number whose factorial you are considering.
  29. |
  30. |What you want is the number of trailing zeroes + 1. 
  31.  
  32. [ clever solution of not-the-posters-problem deleted ... ]
  33.  
  34. No, that's not what he wanted. A little reading before thinking before
  35. coding may come in handy sometimes ;-) What the original poster wants
  36. is the value of the rightmost non-zero digit of n!. 
  37.  
  38. Let D(n) be the value of the rightmost non-zero digit of number n,
  39. it is easy to show that D(n!)= D(D(n)*D((n-1)!). Here's function D,
  40. written in C (it still is c.l.c isn't it?)
  41.  
  42. int D(n) {
  43.  
  44.     while (n && !(n%10))
  45.         n/= 10;
  46.  
  47.  
  48.     return n%10;
  49. }
  50.  
  51. I leave the rest of the program as an exercise for the reader (boy,
  52. I still love writing that ;-)
  53.  
  54. kind regards,
  55.  
  56. Jos aka jos@and.nl
  57. -- 
  58. Atnwgqkrl gy zit vgksr, ug qshiqwtzoeqs!
  59.  
  60.